package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.DmcConstant;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.sleepcare.SleepCareRealDataVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcSleepCareDataService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-23 14:00
 * @Description:
 */
@RestController
@RequestMapping(value = "/sleepCare", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcSleepCareDataController extends BaseController {

    @Resource
    private DmcSleepCareDataService dmcSleepCareDataService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 查询智能床垫实时数据
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/querySleepCareRealDataByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper querySleepCareRealDataByDeviceId(@PathVariable Long deviceId) {

        SleepCareRealDataVo sleepCareRealDataVo = dmcSleepCareDataService
                .querySleepCareRealDataByDeviceId(deviceId);
        if (!PubUtils.isNull(sleepCareRealDataVo.getBindType())) {
            sleepCareRealDataVo.setBind(true);
            switch (sleepCareRealDataVo.getBindType()) {
                case DmcConstant.SLEEP_CARE_BIND_TYPE_OLDMAN :
                    OldmanBedRoomBuildingVo obv = (OldmanBedRoomBuildingVo) redisTemplate
                            .opsForValue().get(RedisKeyUtil.getOmcOldmanId(sleepCareRealDataVo.getOldmanId()));
                    replaceOldmanInfo(sleepCareRealDataVo, obv);
                    break;
                case DmcConstant.SLEEP_CARE_BIND_TYPE_BED :
                    OldmanBedRoomBuildingVo bov = (OldmanBedRoomBuildingVo) redisTemplate
                            .opsForValue().get(RedisKeyUtil.getOmcBedId(sleepCareRealDataVo.getBedId()));
                    replaceOldmanInfo(sleepCareRealDataVo, bov);
                    break;
            }
        }
       // return WrapMapper.ok(sleepCareRealDataVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),sleepCareRealDataVo);
    }

    public void replaceOldmanInfo(SleepCareRealDataVo sleepCareRealDataVo, OldmanBedRoomBuildingVo bov) {

        if (PublicUtil.isNotEmpty(bov)) {

            // 绑定床位, 老人信息和位置根据床位获取
            sleepCareRealDataVo.setOldmanName(bov.getOldmanName());

            if (PublicUtil.isNotEmpty(bov.getRoomNo()) && PublicUtil.isNotEmpty(bov.getBedNo())) {
                sleepCareRealDataVo.setLocation(bov.getRoomNo() + "-" + bov.getBedNo());
            } else {
                sleepCareRealDataVo.setLocation("--");
            }
        }
    }

}
